<?php

declare(strict_types=1);

namespace App\Middleware;

use App\Exception\BaseException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected ContainerInterface $container;


    public function __construct(protected ContainerInterface $container2)
    {
        $this->container = $this->container2;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $token = $request->getQueryParams()['token'] ?? '';

        if (empty($token)) {
            $token = $request->getParsedBody()['token'] ?? '';
        }

        if (empty($token)) {
            $token = $request->getHeader('token')[0] ?? '';
        }

        echo "token: " . $token . "/n";

        // 这里去判断 token 是否有效
        /*if(false){
            throw new BaseException(BaseException::TOKEN_ERROR);
        }else{
            // 缓存管理员信息
        }*/

        return $handler->handle($request);
    }
}
